/*
New Scotland Yard is an online multiplayer adaptation
of the boardgame "Scotland Yard". Copyright (C) 2011
Massey University Software C Group 3
This program is free software: you can redistribute it
and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General
Public License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
package nz.ac.massey.softwarec.group3.session;
import java.util.*;
import nz.ac.massey.softwarec.group3.game.Game;
import nz.ac.massey.softwarec.group3.game.Player;
/**
* GameTracker - Class that looks after all the current games.
* @version 1.0 Release
* @since 1.0
* @authors Natalie Eustace | Wanting Huang | Paul Smith | Craig Spence
*/
public class GameTracker implements GameTrackerInterface {
private final transient List<Game> games = new ArrayList<Game>();
/**
* Getter for the list of current games, that exist but aren't playing.
* @return ArrayList<Game> availableGames - The current existing games.
*/
@Override
public List<Game> getAvailableGames() {
final List<Game> availableGames = new ArrayList<Game>();
for (Game game : games) {
if (!game.isPlaying()) {
availableGames.add(game);
}
}
return availableGames;
}
/**
* Method to find the total number of games.
* @return int numberOfGames - The number of current games.
*/
@Override
public int getCurrentNumberOfAvailableGames() {
int numberOfGames = 0;
for (Game game : games) {
if (!game.isPlaying()) {
numberOfGames++;
}
}
return numberOfGames;
}
/**
* Method to create a new Game.
* @param String creatorEmail - The name of the user who is creating the game.
* @return Game game - The newly created game.
*/
@Override
public Game createNewGame(final String creatorEmail) {
final Game game = new Game(creatorEmail, games.size() + 1);
//game.setTokSessionId();
games.add(game);
return game;
}
/**
* Method to get a game created by a given user.
* @param String creatorEmail - The email of the user who created the game.
* @return Game creatorsGame - The game created by the user.
*/
@Override
public Game getGameMadeByCreator(final String creatorEmail) {
Game creatorsGame = null;
for (int i = 0; i < games.size(); i++) {
if (creatorEmail.equals(games.get(i).getCreatorEmail())) {
creatorsGame = games.get(i);
}
}
return creatorsGame;
}
/**
* Method to get a game with a given user in it.
* @param String email - The email of the user to find.
* @return Game gameWithUser - The game with a given user in it.
*/
@Override
public Game getGameWithUserInIt(final String email) {
Game gameWithUser = null;
for (Game game : games) {
final List<Player> players = game.getPlayers();
for (Player player : players) {
if (player.getPlayerEmail().equals(email)) {
gameWithUser = game;
}
}
}
return gameWithUser;
}
/**
* Checks to see if the game is finished, and if so it removes it from the list of games.
* @param Game game - the game to check.
*/
@Override
public void checkFinishedGame(final Game game) {
if (game.isFinished()) {
games.remove(game);
}
}
}